<?php
/**************************
@Author  : sulwan
@Content : 注册用户
**************************/
include_once(dirname(__file__).'/../init.php');

$action = trim(_g('action'));
switch($action) {
case saveReg:
	saveReg();
	break;
default:
	showIndex();
}

/*默认执行方法*/
function showIndex() {
	global $config;
	include template('register/index');
}



/*保存注册用户*/
function saveReg() {
	$userData['username'] = trim(_g('username'));
	$username = strlen($userData['username']);
	if ( $username<4||$username>15 ){
		$url = './reg.php';
		$time = 3;
		$message = '用户名长度不对,请按照说明输入';
		redirect($url,$time,$message);
    }
	$userInfo['email'] = $userData['email'] = trim(_g('email'));
	if(!Validation::check($userData['email'],'email')) {
		$url = './reg.php';
		$time = 3;
		$message = '请检查邮箱是否输入正确';
		redirect($url,$time,$message);
	}
	if ( !User::checkUserEmail($userData['email']) ){
		$url = './reg.php';
		$time = 3;
		$message = '邮箱已经被使用，请更换一个';
		redirect($url,$time,$message);
	}
	$userData['userPassword'] = trim(_g('userPassword'));
	$userPasswordConfirn = trim(_g('userPasswordConfirn'));
	if($userData['userPassword'] != $userPasswordConfirn && !empty($userData['userPassword'])) {
		$url = './reg.php';
		$time = 3;
		$message = '两次密码输入不同，请从新输入';
		redirect($url,$time,$message);
	}
	$userInfo['userPassword'] = $userData['userPassword'] = md5($userData['userPassword']);
	$authNum = trim(_g('authNum'));
	$authNumService = Session::Get('verify_code');
	if($authNum != $authNumService['default']) {
		$url = './reg.php';
		$time = 3;
		$message = '验证码填写错误，请从新填写';
		redirect($url,$time,$message);
	}
	$userData['roleid'] = 1;
	$user = new User();
	$userInfo['uid'] = $user -> saveReg($userData);
	User::login($userInfo);
	$url = './';
	$time = 3;
	$msg='正在进入管理页';
	redirect($url,$time,$msg);
}
?>